package org.display.util
{
	import org.net.*;
	import org.utils.*;
	import flash.utils.*
		
	public class ModelControl
	{
		private var modelPath:String
		private var modelProxyPath:String
		private var varname:String
		private var vartype:String
		
		public function ModelControl(modelPath:String,varvalue:String)
		{
			varvalue=varvalue.replace(/\s/gis,"")
			this.varname=varvalue.split(":")[0]
			this.vartype=varvalue.split(":")[1]
				
			this.modelPath=modelPath
			modelProxyPath=modelPath.replace(".as","Proxy.as").replace("\\model\\","\\modelProxy\\")
				
			doModel()
			
			doModelProxy()
				
		}
		
		function doModel(){
			
			
			var res:AbcResponder=new AbcResponder()
			
			res.onResult=function(e,path){
				
				var source:String=e
					
				source=replaceSource(source,"//var","public var "+varname+":"+vartype)

				setTimeout(FileControl.saveFile,100,path,source)
				
			}
			
			new AbcHTTPServices(modelPath,res)
			
		}
		
		
		function doModelProxy(){
			
			
			var res:AbcResponder=new AbcResponder()
			
			res.onResult=function(e,path){
				
				var source:String=e
					
				var fun:String="public function get "+varname+"():"+vartype+"{return _model."+varname+"}\n\n"
					          +"      public function set "+varname+"(value:"+vartype+"){ _model."+varname+"=value}\n"
				
				source=replaceSource(source,"//function",fun)
				
				setTimeout(FileControl.saveFile,100,path,source)
				
			}
			
			new AbcHTTPServices(modelProxyPath,res)
			
		}
		
		
		function replaceSource(source:String,regstr:String,newstr:String):String{
		
            if(source.indexOf(newstr)==-1){
			
				source=source.replace(new RegExp(RegControl.transform(regstr),"gis"),regstr+"\n     "+newstr)
			
			}
				
			return source

		}
		

	}
}